﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Business.Portal.ServiceView
{
    public partial class ServiceList : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            // TEST parameter
            //Session["CustomerId"] = "10000";

            // Bind car data to gridview
            BindCustomerService(db, Convert.ToInt32(Session["CustomerId"]));

            if (Page.IsPostBack != true)
            {
                GroupOrderData(Convert.ToInt32(ddlView.SelectedValue));
            }

            // Change gridview pagesize based on dropdownlist
            gvService.SettingsPager.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);

            // Dispose connection to database
            db.Dispose();
        }


        #region Private Functions

        /// <summary>
        /// Function that bind customer cars to gridview
        /// </summary>
        /// <param name="db">Connection to database</param>
        /// <param name="CustomerId">Id on customer</param>
        private void BindCustomerService(TireServiceDB db, int CustomerId)
        {
            // Bind to gridview
            gvService.DataSource = db.GetCustomerService(CustomerId, true);
            gvService.DataBind();
        }

        /// <summary>
        /// Function that change grouping in order gridview
        /// </summary>
        /// <param name="ViewIndex">Integer setting the grouping mode</param>
        protected void GroupOrderData(int ViewIndex)
        {
            gvService.ClearSort();
            gvService.GroupSummary.Clear();
            switch (ViewIndex)
            {
                case 0:
                    gvService.Columns["Name"].Visible = false;

                    break;
                case 1:
                    gvService.Columns["Name"].Visible = false;
                    gvService.GroupBy(gvService.Columns["RegistrationNumber"]);

                    break;
                case 2:
                    gvService.Columns["Name"].Visible = false;
                    gvService.GroupBy(gvService.Columns["ScheduleDate"]);

                    break;
                case 3:
                    gvService.Columns["Name"].Visible = false;
                    gvService.GroupBy(gvService.Columns["WorkCompleted"]);

                    break;
                case 4:
                    gvService.Columns["Name"].Visible = true;
                    gvService.GroupBy(gvService.Columns["Name"]);

                    break;
            }
            gvService.ExpandAll();
        }

        #endregion

        #region Control Functions

        /// <summary>
        /// Button function for export of gridview
        /// </summary>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (ddlExport.Value == "0")
            {
                gvExport.WritePdfToResponse();
            }

            if (ddlExport.Value == "1")
            {
                gvExport.WriteXlsToResponse();
            }

            if (ddlExport.Value == "2")
            {
                gvExport.WriteCsvToResponse();
            }

            if (ddlExport.Value == "3")
            {
                gvExport.WriteRtfToResponse();
            }
        }

        /// <summary>
        /// Dropdown list function that change gridview grouping when selected item is changed
        /// </summary>
        protected void ddlView_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Group data
            GroupOrderData(Convert.ToInt32(ddlView.SelectedValue));
        }

        #endregion
    }
}